/**
 * Created by gongxun on 17/1/19.
 */

var GsbdxTableMicLayer = GameCCSBase.extend({
    Panel_center : null,
    Panel_send : null,
    mMicIndex : null,
    Image_jindus : null,
    mPanels : null,
    mVoiceButton : null,
    mBeganTime : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/GsbdxTableMic.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        this.Panel_send = mo.seekWidgetByName(this.ccsNode, "Panel_send");

        this.loadUI();

        this.Panel_center.setVisible(false);
    },

    loadUI: function () {
        //录音按钮
        this.mVoiceButton = mo.seekWidgetByName(this.ccsNode, "Button_mic");
        this.mVoiceButton.addTouchEventListener(function(sender, type){
            if (type == ccui.Widget.TOUCH_BEGAN){

                sender.setEnabled(false);
                this.mBeganTime = mo.gameHelper.getMillisecond();

                this.showMic(true);
                this.setAudioSendShow(0);
                this.stopTimeOutSend();
                this.scheduleOnce(this.onTimeOutSend, 10);

                var millisecond = mo.gameHelper.getMillisecond().toString();
                var fileName = millisecond + ".amr";
                mo.nativeHelper.playBegan(fileName);

            }else if (type == ccui.Widget.TOUCH_MOVED){
                var pos = sender.getTouchMovePosition();
                pos = mo.gameMainLayer.getTransfromPoint(pos);
                cc.log("pos x:" + pos.x + " y:" + pos.y);

                var bindingbox = sender.getBoundingBox();
                if (cc.rectContainsPoint(bindingbox, pos)){
                    this.setAudioSendShow(0);
                }else{
                    this.setAudioSendShow(1);
                }

            }else if (type == ccui.Widget.TOUCH_ENDED) {
                var isTimeSort = false;
                // cc.log("began:" + this.mBeganTime + "end:" + mo.gameHelper.getMillisecond());
                var jiange = mo.gameHelper.getMillisecond() - this.mBeganTime;
                // cc.log("间隔:" + jiange);
                isTimeSort = jiange > 500;

                //录音文件
                var fileDataArray = null;
                if (this.mFileDataArray != null){
                    fileDataArray = this.mFileDataArray;
                    this.stopTimeOutSend();
                }else{
                    this.stopTimeOutSend();
                    fileDataArray = mo.nativeHelper.playEnd();
                }
                if (fileDataArray.length < 500){
                    isTimeSort = false;
                }

                //太小了不发送
                if (isTimeSort){
                    this.sendAudio(fileDataArray);
                    this.setVoiceEndStatus(false);
                }else{
                    this.setVoiceEndStatus(true);
                }

            }else if (type == ccui.Widget.TOUCH_CANCELED){
                this.stopTimeOutSend();

                var fileDataArray = mo.nativeHelper.playEnd();
                this.setVoiceEndStatus(false);
            }
        }, this);

        //录音进度动画
        this.Image_jindus = [];
        for (var i = 0; i < 7; i++){
            var Image_jindu = mo.seekWidgetByName(this.ccsNode, "Image_jindu" + i);
            this.Image_jindus.push(Image_jindu);
        }

        //panels
        this.mPanels = [];
        this.mPanels.push(mo.seekWidgetByName(this.ccsNode, "Panel_send"));
        this.mPanels.push(mo.seekWidgetByName(this.ccsNode, "Panel_cancel"));
        this.mPanels.push(mo.seekWidgetByName(this.ccsNode, "Panel_short"));
    },

    sendAudio : function(intArray){
        var vmsg = new Int8Array(intArray.length);
        for (var i = 0; i < intArray.length; i++){
            vmsg[i] = intArray[i];
        }
        mo.gameMsgManager.sendUserChatMsg(mo.gameUserInfo.uid, "", 2, vmsg.buffer);
        // var millisecond = mo.gameHelper.getMillisecond().toString();
        // var fileName = millisecond + ".amr";
        // mo.nativeHelper.playFile(fileName, intArray);
    },

    setAudioSendShow : function(index){
        for(var i = 0; i < this.mPanels.length; i++){
            this.mPanels[i].setVisible(index == i);
        }
    },

    showMic : function(isShow){
        this.Panel_center.setVisible(isShow);

        if (isShow){
            this.mMicIndex = 1;
            for (var i = 0; i < 7; i++){
                this.Image_jindus[i].setVisible(false);
            }
            this.schedule(this.onMicAction, 0.3);

            //录音时候关闭
            mo.gameUserInfo.setRecordingForVolume(true);
        }else{
            this.unschedule(this.onMicAction);

            //录音完毕打开
            mo.gameUserInfo.setRecordingForVolume(false);
        }
    },

    //显示录音长度错误
    showVoiceShort : function(isShort){
        if (isShort){
            this.setAudioSendShow(2);
        }else{
            this.showMic(false);
        }
    },

    //录音状态结束
    setVoiceEndStatus : function(isShort){
        var waitTime = isShort ? 2 : 1;
        this.showVoiceShort(isShort);

        var that = this;
        this.mVoiceButton.runAction(cc.sequence(
            cc.delayTime(waitTime),
            cc.callFunc(function(){
                that.mVoiceButton.setEnabled(true);
                that.showMic(false);
            })
        ));
    },

    onMicAction: function(dt){
        cc.log("mic index:" + this.mMicIndex);

        for (var i = 0; i < 7; i++){
            var isShow = (i < this.mMicIndex);
            this.Image_jindus[i].setVisible(isShow);
        }

        this.mMicIndex++;
        if (this.mMicIndex > 7){
            this.mMicIndex = 1;
        }
    },

    //录音超时
    onTimeOutSend : function(dt){
        this.mFileDataArray = mo.nativeHelper.playEnd();
        this.unschedule(this.onMicAction);
    },

    stopTimeOutSend : function(){
        this.mFileDataArray = null;
        this.unschedule(this.onTimeOutSend);
    }
});